/************************************************************************

								Heroes IV
					  Copyright 2002, The 3DO Company

	------------------------------------------------------------------
  						 combat_grid_colors.cpp

	$Header: $

	$NoKeywords: $

 ************************************************************************/

#include "precompiled.h"

#include "combat_grid_colors.h"

#include <stdlib.h>
#include <string>
#include <memory.h>

#include "convert_16_bit.h"
#include "debug_message.h"
#include "elements_of.h"
#include "string_insensitive_compare.h"
#include "table.h"
#include "table_ptr.h"
#include "terrain.h"

namespace
{
	t_combat_grid_colors k_bridge_colors;
	t_combat_grid_colors k_terrain_colors[ k_basic_terrain_count ];
	bool				 g_initialized = false;

	t_table_cache g_grid_table( "combat_grid_colors" );
	std::string const k_bridge_keyword( "bridge" );
	std::string const k_yes_keyword( "yes" );
	std::string const k_no_keyword( "no" );


	enum t_table_columns
	{
		k_column_terrain_keyword = 0,
		k_column_alpha,
		k_column_odd_transparent,
		k_column_odd_red,
		k_column_odd_green,
		k_column_odd_blue,
		k_column_even_transparent,
		k_column_even_red,
		k_column_even_green,
		k_column_even_blue,

		k_column_odd_base = k_column_odd_transparent,
		k_column_even_base = k_column_even_transparent
	};

	enum t_table_column_offsets
	{
		k_column_offset_transparent = 0,
		k_column_offset_red,
		k_column_offset_green,
		k_column_offset_blue,
	};

	void initialize()
	{
		g_initialized = true;

		memset( &k_bridge_colors, 0, sizeof( k_bridge_colors ) );
		memset( &k_terrain_colors, 0, sizeof( k_terrain_colors ) );

		t_table_ptr						table = g_grid_table.get();
		t_table::iterator				line = table->begin();

		line += 2; // First two lines are comments

		while ( line != table->end() )
		{
			std::string keyword = (*line)[k_column_terrain_keyword];

			if ( keyword.empty() )
			{
				line++;
				continue;
			}

			t_combat_grid_colors * current;

			if ( string_insensitive_compare( keyword,  k_bridge_keyword ) == 0 )
			{
				current = &k_bridge_colors;
			}
			else
			{
				t_terrain_type terrain = get_terrain_type( keyword );
				if ( terrain >= k_basic_terrain_count || terrain < 0 )
				{
					current = NULL;
				}
				else
				{
					current = &k_terrain_colors[ terrain ];
				}
			}

			if ( current )
			{
				current->alpha = atoi( (*line)[k_column_alpha].c_str() );

				for ( int i = 0; i < ELEMENTS_OF( current->color ); i++ )
				{
					t_combat_grid_colors::t_color & grid_color = current->color[i];
					int offset = ( i == 0 ? k_column_odd_base : k_column_even_base );

					grid_color.transparent = 
						( string_insensitive_compare( (*line)[ offset + k_column_offset_transparent ], k_yes_keyword ) == 0 );
					
					int red = atoi( (*line)[ offset + k_column_offset_red ].c_str() );
					int green = atoi( (*line)[ offset + k_column_offset_green ].c_str() );
					int blue = atoi( (*line)[ offset + k_column_offset_blue ].c_str() );
					grid_color.color = convert_to_16_bit( red, green, blue );
				}
			}
			else
			{
				DEBUG_MESSAGE( ( "Warning: Unrecognized keyword " + keyword + " in combat_grid table\n" ).c_str() );
			}

			line++;
		}
	}
};


// Return the colors needed for a given terrain type
t_combat_grid_colors const & get_combat_grid_colors( t_terrain_type main_terrain_type,
													 bool			on_bridge )
{
	if ( !g_initialized )
		initialize();

	if ( on_bridge )
		return k_bridge_colors;

	t_terrain_type basic_terrain = get_basic_terrain( main_terrain_type );
	assert( basic_terrain >= 0 && basic_terrain < ELEMENTS_OF( k_terrain_colors ) );

	if ( basic_terrain >= 0 && basic_terrain < ELEMENTS_OF( k_terrain_colors ) )
	{
		return k_terrain_colors[ basic_terrain ];
	}
	else
	{
		return k_terrain_colors[ 0 ];
	}
}
